<template>
  <div class="login-container">
    <el-card class="box-card">
      <div class="login-header">
        <h2 class="login-title">用户管理系统</h2>
        <p class="login-subtitle">请输入您的账号信息</p>
      </div>

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label-width="0" prop="username" class="login-form-item">
          <el-input
              placeholder="请输入用户名"
              v-model="ruleForm.username"
              clearable
              class="login-input"
          ></el-input>
        </el-form-item>
        <el-form-item label-width="0" prop="password" class="login-form-item">
          <el-input
              placeholder="请输入密码"
              v-model="ruleForm.password"
              show-password
              class="login-input"
          ></el-input>
        </el-form-item>

        <div class="login-actions">
          <el-button
              @click="submitForm('ruleForm')"
              class="login-button login-button-primary"
          >
            登录
          </el-button>
          <el-button class="login-button login-button-secondary">
            注册
          </el-button>
        </div>

        <div class="login-links">
          <a href="#" class="login-link">忘记密码?</a>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {userLogin} from "@/api/user";
import {mapState} from "vuex";

export default {
  name: "",
  data() {
    return {
      ruleForm:{
        username: '',
        password: '',
      },

      rules: {
        username:[{
          required: true, message: '请输入用户名', trigger: 'blur'
        }],
        password: [{
          required: true, message: '请输入密码', trigger: 'blur'
        },{
          min:5, message: '密码必须不小于5位', trigger: 'blur'
        }]
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          userLogin(this.ruleForm).then(res => {
            if (res.code === 200){
              this.$message({
                message: '登录成功',
                type: 'success'
              });
              this.$router.push({
                path:'/home',
              });
              this.$store.commit('setUser',res.data);
            }else {
              this.$message.error('请输入正确的用户名和密码');//未查询到数据时返回
            }
          }).catch(err => {
            this.$message.error('请输入正确的用户名和密码');//后端代码错误时返回
          })
        } else {
          this.$message.error('请输入正确的用户名和密码');//验证不通过时返回
          return false;
        }
      });
    },
  },
  computed:{
    ...mapState(['user']),
  }
}
</script>

<style scoped>
/* 登录页面容器 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 89vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

/* 卡片样式 */
.box-card {
  width: 480px;
  padding: 0 20px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.box-card:hover {
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  transform: translateY(-5px);
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
}

/* 表单样式 */
.demo-ruleForm {
  margin-top: 20px;
}

.login-form-item {
  margin-bottom: 20px;
}

.login-input {
  //height: 48px;
  border-radius: 8px;
  //border: 1px solid #e0e0e0;
  transition: all 0.3s ease;
}

.login-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
}

/* 按钮样式 */
.login-actions {
  display: flex;
  gap: 12px;
  margin-top: 30px;
}

.login-button {
  flex: 1;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-button-primary {
  background-color: #667eea;
  border-color: #667eea;
  color: white;
}

.login-button-primary:hover {
  background-color: #5a6edb;
  border-color: #5a6edb;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.login-button-secondary {
  background-color: #f5f5f5;
  border-color: #e0e0e0;
  color: #333;
}

.login-button-secondary:hover {
  background-color: #eeeeee;
  border-color: #d0d0d0;
  transform: translateY(-2px);
}

/* 链接样式 */
.login-links {
  text-align: center;
  margin-top: 20px;
}

.login-link {
  color: #667eea;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.3s ease;
}

.login-link:hover {
  color: #5a6edb;
  text-decoration: underline;
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.box-card {
  animation: fadeIn 0.5s ease forwards;
}

.login-form-item {
  animation: fadeIn 0.5s ease forwards;
}

.login-form-item:nth-child(1) { animation-delay: 0.1s; opacity: 0; }
.login-form-item:nth-child(2) { animation-delay: 0.2s; opacity: 0; }
.login-actions { animation-delay: 0.3s; opacity: 0; animation: fadeIn 0.5s ease 0.3s forwards; }
.login-links { animation-delay: 0.4s; opacity: 0; animation: fadeIn 0.5s ease 0.4s forwards; }
</style>
